import { getClassList } from '@/api/score'

const state = {
  classList: []
}

const mutations = {
  SET_CLASS_LIST(state, list) {
    state.classList = list || []  // 确保始终是数组
  }
}

const actions = {
  async fetchClassList({ commit }, examId) {
    try {
      const res = await getClassList(examId)
      if (res.code === 200 && Array.isArray(res.data)) {  // 检查返回数据
        commit('SET_CLASS_LIST', res.data)
        return res.data
      } else {
        console.error('获取班级列表数据格式错误:', res)
        commit('SET_CLASS_LIST', [])
        return []
      }
    } catch (error) {
      console.error('获取班级列表失败:', error)
      commit('SET_CLASS_LIST', [])
      return []
    }
  }
}

const classModule = {
  namespaced: true,
  state,
  mutations,
  actions
}

export default classModule 